﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OMSService
{
    public class Helpers
    {
        public static void CreateRoles()
        {
            using (OMSModelContainer context = new OMSModelContainer())
            {
                Role admin = new Role
                {
                    UserRole = "admin"
                };
                Role super = new Role
                {
                    UserRole = "super"
                };
                Role merch = new Role
                {
                    UserRole = "merch"
                };
                Role customer = new Role
                {
                    UserRole = "customer"
                };
                context.Roles.AddObject(admin);
                context.Roles.AddObject(super);
                context.Roles.AddObject(merch);
                context.Roles.AddObject(customer);
                context.SaveChanges();
            }
        }

        public static void CreateUser(string firstName, string lastName, string email, string login, string password, string role)
        {
            using (OMSModelContainer context = new OMSModelContainer())
            {
                string salt = UserModel.GenerateSalt();
                Role userRole = (from m in context.Roles where m.UserRole == role select m).FirstOrDefault();
                if (userRole == null)
                {
                    Helpers.CreateRoles();
                    userRole = (from m in context.Roles where m.UserRole == role select m).FirstOrDefault();
                }

                User user = new User
                {
                    Email = email,
                    FirstName = firstName,
                    LastName = lastName,
                    Login = login,
                    Region = "North",
                    Role = userRole,
                    Hash = UserModel.MakeHash(password, salt),
                    Salt = salt,
                    IsLocked = false
                };
                if (role == "customer")
                {
                    Customer customer = new Customer
                    {
                        Balance = 0,
                        Type = "default"
                    };
                    user.Customer = customer;
                }
                context.Users.AddObject(user);
                context.SaveChanges();
            }
        }

        public static void CreateItem(string name, string description, decimal price)
        {
            using (OMSModelContainer context = new OMSModelContainer())
            {
                Item item = new Item
                {
                    Description = description,
                    Name = name,
                    Price = price,
                    Quantity = 100
                };
                context.Items.AddObject(item);
                context.SaveChanges();
            }
        }


        public static void CreateSampleData()
        {
            using (OMSModelContainer context = new OMSModelContainer())
            {
                foreach (Customer c in context.Customers)
                    context.DeleteObject(c);

                foreach (User user in context.Users)
                    context.DeleteObject(user);
                
                foreach (Item item in context.Items)
                    context.DeleteObject(item);
                context.SaveChanges();
            }
            
            CreateUser("admin", "admin", "admin@oms.com", "admin", "admin", "admin");
            CreateUser("root", "root", "root@oms.com", "root", "root", "admin");
            CreateUser("supervisor", "supervisor", "supervisor@oms.com", "supervisor", "supervisor", "super");
            
            CreateUser("merchandiser1", "merchandiser1", "merchandiser1@oms.com", "merchandiser1", "merchandiser1", "merch");
            CreateUser("merchandiser2", "merchandiser2", "merchandiser2@oms.com", "merchandiser2", "merchandiser2", "merch");

            CreateUser("customer1", "customer1", "customer1@oms.com", "customer1", "customer1", "customer");
            CreateUser("customer2", "customer2", "customer2@oms.com", "customer2", "customer2", "customer");
            CreateUser("customer3", "customer3", "customer3@oms.com", "customer3", "customer3", "customer");

            for (int i = 1; i < 100; ++i)
            {
                CreateItem("testItem" + i.ToString(), "description" + i.ToString(), (decimal) i);
            }
        }
    }
}